package com.example.pt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pt.entity.RecommendationFeedback;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface RecommendationFeedbackMapper extends BaseMapper<RecommendationFeedback> {
    
    /**
     * 获取用户的所有不感兴趣的种子ID
     * @param userId 用户ID
     * @return 不感兴趣的种子ID列表
     */
    @Select("SELECT torrent_id FROM recommendation_feedback WHERE user_id = #{userId} AND feedback_type = 0")
    List<Integer> getUserDislikedTorrentIds(@Param("userId") Integer userId);
    
    /**
     * 获取用户对各类别的负反馈统计
     * @param userId 用户ID
     * @return 类别和负反馈次数的映射
     */
    @Select("SELECT t.category, COUNT(*) as dislike_count " +
           "FROM recommendation_feedback f " +
           "JOIN torrentfile t ON f.torrent_id = t.tid " +
           "WHERE f.user_id = #{userId} " +
           "GROUP BY t.category")
    List<Map<String, Object>> getUserCategoryDislikeStats(@Param("userId") Integer userId);
    
    /**
     * 统计指定时间段内各种反馈类型的数量
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 反馈类型和数量的映射
     */
    @Select("SELECT feedback_type, COUNT(*) as count " +
           "FROM recommendation_feedback " +
           "WHERE create_time BETWEEN #{startTime} AND #{endTime} " +
           "GROUP BY feedback_type")
    List<Map<String, Object>> countFeedbackTypesByTimeRange(
            @Param("startTime") Date startTime, 
            @Param("endTime") Date endTime);
    
    /**
     * 检查用户是否已经对某个种子提交过特定类型的反馈
     * @param userId 用户ID
     * @param torrentId 种子ID
     * @param feedbackType 反馈类型
     * @return 反馈数量
     */
    @Select("SELECT COUNT(*) FROM recommendation_feedback " +
           "WHERE user_id = #{userId} AND torrent_id = #{torrentId} AND feedback_type = #{feedbackType}")
    int checkUserFeedbackExists(@Param("userId") Integer userId, @Param("torrentId") Integer torrentId, @Param("feedbackType") Integer feedbackType);
} 